package com.pega.rasmus.tween;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

/**
 * 
 * @author Aina.huang E-mail: 674023920@qq.com
 *
 * code from http://dev.firnow.com/course/3_program/java/javajs/20100719/459467.html
 */
public class GameView extends View {
    /** Called when the activity is first created. */
	private Paint mPaint = null;
	private Animation mAlphaAnimation = null;
	private Animation mScaleAnimation = null;
	private Animation mTranslateAnimation = null;
	private Animation mRotateAnimation = null;
	private Bitmap mBitmap = null;
	private Bitmap mBackground = null;
	
	public GameView(Context context) {
		super(context);
		mBitmap = ((BitmapDrawable) this.getResources().getDrawable(
				R.drawable.img)).getBitmap();
		mBackground = ((BitmapDrawable) this.getResources().getDrawable(
				R.drawable.back)).getBitmap();
	}
	
    @Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		
		canvas.drawBitmap(mBackground, 0, 0, mPaint);
		canvas.drawBitmap(mBitmap, 20, 40, mPaint);
    }
	
    public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_UP:
			// transparency
			mAlphaAnimation = new AlphaAnimation(0.1f, 1.0f);
			mAlphaAnimation.setDuration(3000);
			this.startAnimation(mAlphaAnimation);
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			// scale / zoom in/out the whole screen 0.0 to 1.0
			mScaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f,
					1.0f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.0f);
			mScaleAnimation.setDuration(3000);
			this.startAnimation(mScaleAnimation);
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			// move
			mTranslateAnimation = new TranslateAnimation(0, 100, 0, 100);
			mTranslateAnimation.setDuration(2000);
			this.startAnimation(mTranslateAnimation);
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			// rotate
			mRotateAnimation = new RotateAnimation(0.0f, 360.0f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			mRotateAnimation.setDuration(3000);
			this.startAnimation(mRotateAnimation);
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
}
